﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HMS.BL;
using HMS.Common;
using HMS.Logging;
using System.Data.SqlClient;
using System.Web.Script.Serialization;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Collections.Specialized;

namespace HMS.DL
{
    public class ReportData
    {

        private const string ClsName = "$ReportData$";
        public string dimTableName { get; set; }
        public int ListId { get; set; }
        public string SrcString { get; set; }
        public string queryRel { get; set; }
        public string sidx { get; set; }
        public string sord { get; set; }
        public int rows { get; set; }
        public int page { get; set; }
        public int limit { get; set; }
        public string query { get; set; }
        public string queryObj { get; set; }
        public bool _search { get; set; }
        public string SourceQuery { get; set; }
        public string selectedColumns { get; set; }

        public bool bShowSource { get; set; }

        //SrcString 
        //queryRel

        public string GetGridColdata()
        {
            try
            {

                string primarykey = string.Empty;
                Common objcom = new Common();

                Connection sqlConn = new Connection();
                //string strQry = " SELECT * FROM " + dimTableName;
                string strQry = string.Empty;

                List<DBParameter> lstparam = new List<DBParameter>();
                lstparam.Add(new DBParameter("@ListId", DbType.Int32, ParameterDirection.Input, 0, ListId));
                lstparam.Add(new DBParameter("@bShowSource", DbType.Boolean, ParameterDirection.Input, 0, bShowSource));
                lstparam.Add(new DBParameter("@selectedColumns", DbType.String, ParameterDirection.Input,selectedColumns));

                Util.WritInfoLog(ClsName, "GetGridColdata", "method for GetGridColdata");
                DataSet ds = sqlConn.GetDataSetFromFactoryProc("usp_GetGridColdataReport", ref(lstparam));
                Util.WritInfoLog(ClsName, "GetGridColdata", "End method for GetGridColdata");

                //original
                //if (bShowSource == false)
                //{
                //    strQry = " SELECT * FROM X_MASTER_LIST_COLUMNS ";
                //    strQry = strQry + " WHERE LIST_ID= " + ListId;
                //}
                //else
                //{
                //    strQry = " SELECT * FROM X_MASTER_LIST_COLUMNS ";
                //    strQry = strQry + " WHERE LIST_ID= " + ListId;
                //    strQry = strQry + " AND FIELD_NAME IN ( ";
                //    strQry = strQry + selectedColumns + " ) ";

                //    strQry = strQry + " UNION ";
                //    strQry = strQry + " SELECT * FROM X_MASTER_LIST_COLUMNS ";
                //    strQry = strQry + " WHERE LIST_ID= " + ListId;
                //    strQry = strQry + " AND IS_PRIMARY='True' ";
                //    strQry = strQry + " UNION ";
                //    strQry = strQry + " SELECT * FROM X_MASTER_LIST_COLUMNS ";
                //    strQry = strQry + " WHERE LIST_ID= " + ListId;
                //    strQry = strQry + " AND FIELD_NAME='Id' ";
                //}

                //Util.WritInfoLog("$ReportData$", "GetGridColdata", "Query for GetGridColdata", string.Format("Query:{0}", strQry));
                //DataSet ds = sqlConn.GetDataSetFromFactory(strQry);
                //Util.WritInfoLog("$ReportData$", "GetGridColdata", "End Query for GetGridColdata");

                DataTable dt = ds.Tables[0];

                List<colModel> listColmodel = new List<colModel>();
                List<string> columns = new List<string>();

                int index = 0;
                //
                if (bShowSource == true)
                {
                    columns.Add("ROW");
                    colModel colmodel3 = new colModel();
                    colmodel3.index = "ROW";
                    colmodel3.name = "ROW";
                    colmodel3.hidden = true;
                    listColmodel.Add(colmodel3);

                    columns.Add("SOURCE");
                    colModel colmodel1 = new colModel();
                    colmodel1.index = "SOURCE";
                    colmodel1.name = "SOURCE";
                    listColmodel.Add(colmodel1);
                }
                //
                //
                foreach (DataRow dr in ds.Tables[0].Rows)
                {

                    //for columns
                    columns.Add(dr["DISPLAY"].ToString());

                    //for colmodel
                    colModel colmodel = new colModel();

                    colmodel.index = dr["FIELD_NAME"].ToString();
                    colmodel.name = dr["FIELD_NAME"].ToString();
                    colmodel.width = 150;

                    Dictionary<string, object> edoptions = new Dictionary<string, object>();
                    Dictionary<string, object> editrules = new Dictionary<string, object>();
                    Dictionary<string, object> formatoptions = new Dictionary<string, object>();

                    if (Convert.ToString(dr["PRECISION"]) != "")
                    {
                        //edoptions.Add("size", Convert.ToInt32(dr["PRECISION"]));
                        //edoptions.Add("width", Convert.ToInt32(dr["PRECISION"]));
                        edoptions.Add("maxlength", Convert.ToInt32(dr["PRECISION"]));
                    }

                    if (Convert.ToString(dr["IS_REQUIRED"]).ToLower() == "true")
                    {
                        editrules.Add("required", true);
                    }

                    if (dr["IS_PRIMARY"].ToString().ToLower() == "true")
                    {

                        primarykey = dr["FIELD_NAME"].ToString();
                        //colmodel.editable = false;//sorttype:'int'
                        //colmodel.sorttype = "int";//depending on datatype do it from X_MASTER_LIST_COLUMNS
                        // Dictionary<string, Boolean> edoption = new Dictionary<string, Boolean>();
                        //editrules:{required:true, edithidden:true}, 
                        //colmodel.hiddden = true;
                        //colmodel.editoptions = edoptions;
                        edoptions.Add("required", true);
                        edoptions.Add("edithidden", true);
                        colmodel.key = true;
                        colmodel.editable = false;
                        //editrules.Add("edithidden", true); 
                    }
                    else
                    {
                        colmodel.editable = true;
                    }

                    if (dr["DATA_TYPE"].ToString().ToLower() == "number") //integer for whole number
                    {
                        colmodel.sorttype = "int";
                        colmodel.align = "right";
                        editrules.Add("number", true);
                        edoptions.Add("NullIfEmpty", true);
                        //colmodel.NullIfEmpty = "true";                        
                        //formatter 
                        colmodel.formatter = "number";
                        //format option for number
                        formatoptions.Add("decimalSeparator", ".");
                        formatoptions.Add("defaulValue", 0);
                        formatoptions.Add("decimalPlaces", 2);
                    }
                    else if (dr["DATA_TYPE"].ToString().ToLower() == "varchar")
                    {
                        //colmodel.sorttype = "text";
                        colmodel.sorttype = "string";
                        colmodel.align = "left";
                        //colmodel.stype = "string";
                    }
                    else if (dr["DATA_TYPE"].ToString().ToLower() == "date")
                    {
                        colmodel.sorttype = "date";
                        colmodel.align = "right";
                        //edoptions.Add("dataInit","function(el) {  $(el).datepicker({ dateFormat: 'mm-dd-yy' });");
                        //edoptions.Add("dataInit"," $(el).datepicker();");
                        editrules.Add("date", true);
                        colmodel.formatter = "date";
                        ////date format in mm/dd/yyyy
                        formatoptions.Add("srcformat", "ISO8601Long");
                        formatoptions.Add("newformat", "m/d/Y");
                        //edoptions.Add("type", "date");
                        //  edoptions.Add("editoptions", "js:function(elm){}");                         
                        // "editoptions"=>array("editoptions"=> 
                        //"js:function(elm){setTimeout(function(){ 
                        //    jQuery(elm).datepicker({dateFormat:'yy-mm-dd'}); 
                        //    jQuery('.ui-datepicker').css({'font-size':'75%'}); 
                        //},200);}") 
                    }
                    else if (dr["DATA_TYPE"].ToString().ToLower() == "float")
                    {
                        colmodel.sorttype = "float";
                        colmodel.align = "right";
                    }

                    //control type
                    if (dr["CONTROL_TYPE"].ToString().ToLower() == "textarea")
                    {
                        colmodel.edittype = dr["CONTROL_TYPE"].ToString().ToLower();
                        edoptions.Add("rows", "2");
                        edoptions.Add("cols", "20");
                    }
                    else if (dr["CONTROL_TYPE"].ToString().ToLower() == "checkbox")
                    {
                        colmodel.edittype = "checkbox";//,editoptions:{value:"Yes:No"} 
                        edoptions.Add("value", "Yes:No");
                        colmodel.formatter = "checkbox";
                    }
                    else if (dr["CONTROL_TYPE"].ToString().ToLower() == "dropdown")
                    {
                        colmodel.edittype = "select";//editoptions:{value:"FE:FedEx;TN:TNT"}
                        //colmodel.formatter = "select";
                        //objcom.formDropdownString
                        string drpval = objcom.formDropdownString(dr["DROPDOWN_VAL"].ToString());
                        edoptions.Add("value", drpval); //values in this format
                        edoptions.Add("style", "width:160px");
                        // edoptions.Add("value", "0:Select;FE:FedEx;TN:TNT"); //values in this format
                        //edoptions.Add("size", 400);
                    }

                    if (colmodel.name == "Id")
                    {
                        colmodel.hidden = true;
                    }
                    colmodel.editoptions = edoptions;
                    colmodel.editrules = editrules;
                    colmodel.formatoptions = formatoptions;

                    listColmodel.Add(colmodel);
                    index++;
                }

                //
                if (bShowSource == true)
                {
                    columns.Add("Priority");
                    colModel colmodel2 = new colModel();
                    colmodel2.index = "Priority";
                    colmodel2.name = "Priority";
                    colmodel2.hidden = true;
                    listColmodel.Add(colmodel2);
                }
                //

                Grid griddata = new Grid();
                griddata.colModelList = listColmodel;
                griddata.columns = columns;
                griddata.primarykey = primarykey;

                JavaScriptSerializer js = new JavaScriptSerializer();
                return js.Serialize(griddata);

            }
            catch (Exception ex)
            {
                Util.WritErrorLog("$MasterData$", "GetGriddata", "Error in GetGriddata", "", ex);
                throw ex;
            }
        }

        public string GetGriddata()
        {
            try
            {

                string primarykey = string.Empty;
                string strData = string.Empty;

                Connection sqlConn = new Connection();
                int start = ((page - 1) * rows);

                List<DBParameter> lstparam = new List<DBParameter>();
                lstparam.Add(new DBParameter("@ListId", DbType.Int32, ParameterDirection.Input, 0, ListId));
                lstparam.Add(new DBParameter("@dimTableName", DbType.String, ParameterDirection.Input, 255, dimTableName));
                lstparam.Add(new DBParameter("@selectedColumns", DbType.String, ParameterDirection.Input, selectedColumns));
                lstparam.Add(new DBParameter("@queryRel", DbType.String, ParameterDirection.Input,2000, queryRel));
                lstparam.Add(new DBParameter("@SourceQuery", DbType.String, ParameterDirection.Input, 2000, SourceQuery));
                lstparam.Add(new DBParameter("@start", DbType.Int32, ParameterDirection.Input, 0, start));
                lstparam.Add(new DBParameter("@rows", DbType.Int32, ParameterDirection.Input, 0, rows));

                Util.WritInfoLog(ClsName, "GetGriddata", "method for GetGriddata");
                DataSet dsdata = sqlConn.GetDataSetFromFactoryProc("usp_GetGriddataReport", ref(lstparam));
                Util.WritInfoLog(ClsName, "GetGriddata", "End method for GetGriddata");

                page = 1;
                //do for paging 
                DataTable dtData = dsdata.Tables[0];
                Int32 cnt = 0;
                //cnt = Convert.ToInt32(dtData.Rows.Count);
                cnt = Convert.ToInt32(dsdata.Tables[1].Rows[0]["CNT"]);

                strData = Common.JsonForJqgrid(dtData, rows, cnt, page);              

                return strData;
            }
            catch (Exception ex)
            {
                Util.WritErrorLog("$MasterData$", "GetGriddata", "Error in GetGriddata", "", ex);
                throw ex;
            }
        }

        //before SP
        //public string GetGriddata()
        //{
        //    try
        //    {

        //        string primarykey = string.Empty;
        //        string strData = string.Empty;

        //        Connection sqlConn = new Connection();

        //        string strColQry = " SELECT FIELD_NAME,IS_PRIMARY FROM X_MASTER_LIST_COLUMNS ";
        //        strColQry = strColQry + " WHERE LIST_ID= " + ListId;
        //        //strColQry = strColQry + " AND IS_PRIMARY ='TRUE' ";

        //        strColQry = strColQry + "SELECT * FROM X_MASTER_LIST ";
        //        strColQry = strColQry + " WHERE LIST_ID= " + ListId;


        //        Util.WritInfoLog("$ReportData$", "GetGriddata", "Query for get details in GetGriddata", string.Format("Query:{0}", strColQry));
        //        DataSet ds = sqlConn.GetDataSetFromFactory(strColQry);
        //        Util.WritInfoLog("$ReportData$", "GetGriddata", "End Query for get details in GetGriddata");

        //        DataTable dt = ds.Tables[0];
        //        DataTable dtX = ds.Tables[1];
        //        //remove dim_id
        //        //dt.Columns.Remove("DIM_ID");

        //        //DataTable dtCol = ds.Tables[1];
        //        //DataView DvCol = new DataView(dtCol);
        //        // DataView DvColPrimary = new DataView(dtCol);

        //        //DvColPrimary.RowFilter = " IS_PRIMARY =TRUE";
        //        //// DvCol.RowFilter = " IS_PRIMARY =TRUE";

        //        if (dt.Rows.Count > 0)
        //        {

        //            DataTable dtcol = ds.Tables[0];
        //            DataView dv = new DataView(dtcol);
        //            dv.RowFilter = " IS_PRIMARY =FALSE ";

        //            var names = from DataRowView row in dv
        //                        select (string)row["FIELD_NAME"];
        //            string[] arr = names.ToArray();
        //            string cols = string.Join(", ", arr);

        //            StringBuilder SB = new StringBuilder();
        //            foreach (DataRowView dr in dv)
        //            {
        //                SB.Append("Dim." + dr["FIELD_NAME"].ToString() + ", ");
        //            }

        //            string strCols = SB.ToString().Substring(0, SB.ToString().Length - 2);

        //            DataView dvPrimary = new DataView(ds.Tables[0]);
        //            dvPrimary.RowFilter = " IS_PRIMARY =TRUE ";

        //            if (sidx == "" || sidx == null)
        //            {
        //                sidx = dvPrimary[0]["FIELD_NAME"].ToString();                        
        //            }

        //            string sortExp = sidx + " " + sord;
        //            int start = ((page - 1) * rows);

        //            string strDrop = " IF OBJECT_ID('tempdb..#tempRpt') IS NOT NULL DROP TABLE #tempRpt ; ";

        //            Util.WritInfoLog("$ReportData$", "GetGriddata", "Query for dropping temp table", string.Format("Query:{0}", strDrop));
        //            int ret = sqlConn.SaveDataByFactory(strDrop);
        //            Util.WritInfoLog("$ReportData$", "GetGriddata", "End Query for dropping temp table");
                 

        //            //for getting selected columns
        //            string filteredcolumns = "Row,Source,Id," + dvPrimary[0]["FIELD_NAME"].ToString() + "," + selectedColumns + ",Priority";
        //            //string filteredcolumns = "Row,Source,Id," + selectedColumns + ",Priority";


        //            string strQry = " SELECT '' AS SOURCE,DIM_ID," + cols + " into #tempRpt FROM " + dimTableName + " WHERE ";
        //            strQry = strQry + dvPrimary[0]["FIELD_NAME"].ToString() + " IN ( SELECT MASTER_ID FROM ";
        //            strQry = strQry + dtX.Rows[0]["TABLE_SOURCE"];
        //            if (queryRel != "")
        //            {
        //                strQry = strQry + "  WHERE " + queryRel.Replace("\"", "'");  //+" CRM > 0 AND ERP = 0 AND WEB = 0 "; //
        //            }

        //            if (SourceQuery != "")
        //            {
        //                if (queryRel != "")
        //                {
        //                    strQry = strQry + "  AND " + SourceQuery;  //+" CRM > 0 AND ERP = 0 AND WEB = 0 "; //
        //                }
        //                else
        //                {
        //                    strQry = strQry + "  WHERE " + SourceQuery;  //+" CRM > 0 AND ERP = 0 AND WEB = 0 "; //
        //                }
        //            }

        //            strQry = strQry + "  ) ";


        //            strQry = strQry + "SELECT " + filteredcolumns + " FROM (";
        //            strQry = strQry + " SELECT ROW_NUMBER() OVER (ORDER BY " + dvPrimary[0]["FIELD_NAME"].ToString();
        //            strQry = strQry + " , priority " + ") AS Row,* FROM (";
        //            strQry = strQry + " (select Source, DIM_ID," + cols + ",0 as priority from #tempRpt ";
        //            strQry = strQry + " UNION ";
        //            strQry = strQry + " Select Dim.Source,Dim.MASTER_ID," + strCols + ",1 as priority from " + dtX.Rows[0]["TABLE_XREF"];
        //            //strQry = strQry + "  Dim INNER JOIN #tempRpt t " + " ON Dim." + dvPrimary[0]["FIELD_NAME"].ToString();
        //            strQry = strQry + "  Dim INNER JOIN #tempRpt t " + " ON Dim.MASTER_ID";
        //            strQry = strQry + " = t." + dvPrimary[0]["FIELD_NAME"].ToString() + ") ";
        //            //strQry = strQry + " order by " + dvPrimary[0]["FIELD_NAME"].ToString() + " , priority ";
        //            strQry = strQry + ")tmp";
        //            strQry = strQry + " ) temp ";
        //            //strQry = strQry + " WHERE Row > " + start + " AND Row <= " + (start + rows) + ";";                          

        //            //For Count                 
        //            //strQry = strQry + " SELECT '' AS SOURCE," + cols + " into #tempRpt FROM " + dimTableName + " WHERE ";
        //            //strQry = strQry + dvPrimary[0]["FIELD_NAME"].ToString() + " IN ( SELECT MASTER_ID FROM ";
        //            //strQry = strQry  + dtX.Rows[0]["TABLE_SOURCE"];
        //            //strQry = strQry + "  WHERE " + SourceQuery;  //+" CRM > 0 AND ERP = 0 AND WEB = 0 "; //
        //            //strQry = strQry + "  ) ";

        //            //strQry = strQry = "SELECT COUNT(*) CNT FROM (";
        //            //strQry = strQry + " SELECT ROW_NUMBER() OVER (ORDER BY " + dvPrimary[0]["FIELD_NAME"].ToString();
        //            //strQry = strQry + " , priority " + ") AS Row,* FROM (";

        //            //strQry = strQry + " (select Source, " + cols + ",0 as priority from #tempRpt ";
        //            //strQry = strQry + " UNION ";
        //            //strQry = strQry + " Select Dim.Source," + strCols + ",1 as priority from " + dtX.Rows[0]["TABLE_XREF"];
        //            //strQry = strQry + "  Dim INNER JOIN #tempRpt t " + " ON Dim." + dvPrimary[0]["FIELD_NAME"].ToString();
        //            //strQry = strQry  + " = t." + dvPrimary[0]["FIELD_NAME"].ToString() + ") ";
        //            ////strQry = strQry + " order by " + dvPrimary[0]["FIELD_NAME"].ToString() + " , priority ";
        //            //strQry = strQry + ")tmp";
        //            //strQry = strQry + " ) temp ";

        //            Util.WritInfoLog("$ReportData$", "GetGriddata", "Query for GetGriddata", string.Format("Query:{0}", strQry));
        //            DataSet dsdata = sqlConn.GetDataSetFromFactory(strQry);
        //            Util.WritInfoLog("$ReportData$", "GetGriddata", "End Query for GetGriddata");

        //            page = 1;
        //            //do for paging 
        //            DataTable dtData = dsdata.Tables[0];
        //            Int32 cnt = 0;
        //            // cnt = Convert.ToInt32(dsdata.Tables[1].Rows[0]["CNT"]);                   
        //            cnt = Convert.ToInt32(dtData.Rows.Count);

        //            strData = Common.JsonForJqgrid(dtData, rows, cnt, page);

        //        }

        //        //JavaScriptSerializer js = new JavaScriptSerializer();
        //        return strData;

        //    }
        //    catch (Exception ex)
        //    {
        //        Util.WritErrorLog("$MasterData$", "GetGriddata", "Error in GetGriddata", "", ex);
        //        throw ex;
        //    }
        //}


        public DataSet GetGriddataDummy()
        {
            try
            {               

                Connection sqlConn = new Connection();

                List<DBParameter> lstparam = new List<DBParameter>();
                lstparam.Add(new DBParameter("@ListId", DbType.Int32, ParameterDirection.Input, 0, ListId));
                lstparam.Add(new DBParameter("@dimTableName", DbType.String, ParameterDirection.Input, 255, dimTableName));
                lstparam.Add(new DBParameter("@selectedColumns", DbType.String, ParameterDirection.Input, selectedColumns));
                lstparam.Add(new DBParameter("@queryRel", DbType.String, ParameterDirection.Input,2000, queryRel));
                lstparam.Add(new DBParameter("@SourceQuery", DbType.String, ParameterDirection.Input, 2000, SourceQuery));
                lstparam.Add(new DBParameter("@export", DbType.Boolean, ParameterDirection.Input, 2000, true));
                //@export

                Util.WritInfoLog(ClsName, "GetGriddata", "method for GetGriddata");
                DataSet dsdata = sqlConn.GetDataSetFromFactoryProc("usp_GetGriddataReport", ref(lstparam));
                Util.WritInfoLog(ClsName, "GetGriddata", "End method for GetGriddata");
               
                return dsdata;
            }
            catch (Exception ex)
            {
                Util.WritErrorLog("$MasterData$", "GetGriddata", "Error in GetGriddata", "", ex);
                throw ex;
            }
        }


        //original before SP
        //public DataSet GetGriddataDummy()
        //{
        //    try
        //    {

        //        string primarykey = string.Empty;
        //        string strData = string.Empty;
        //        DataSet dsdata = new DataSet();

        //        Connection sqlConn = new Connection();

        //        string strColQry = " SELECT FIELD_NAME,IS_PRIMARY FROM X_MASTER_LIST_COLUMNS ";
        //        strColQry = strColQry + " WHERE LIST_ID= " + ListId;

        //        strColQry = strColQry + "SELECT * FROM X_MASTER_LIST ";
        //        strColQry = strColQry + " WHERE LIST_ID= " + ListId;

        //        Util.WritInfoLog("$ReportData$", "GetGriddataDummy", "Query for get details in GetGriddataDummy", string.Format("Query:{0}", strColQry));
        //        DataSet ds = sqlConn.GetDataSetFromFactory(strColQry);
        //        Util.WritInfoLog("$ReportData$", "GetGriddataDummy", "End Query for get details in GetGriddataDummy");

        //        DataTable dt = ds.Tables[0];
        //        DataTable dtX = ds.Tables[1];

        //        if (dt.Rows.Count > 0)
        //        {
        //            DataTable dtcol = ds.Tables[0];
        //            DataView dv = new DataView(dtcol);

        //            var names = from DataRow row in dt.Rows
        //                        select (string)row["FIELD_NAME"];
        //            string[] arr = names.ToArray();
        //            string cols = string.Join(", ", arr);

        //            StringBuilder SB = new StringBuilder();
        //            foreach (DataRowView dr in dv)
        //            {
        //                SB.Append("Dim." + dr["FIELD_NAME"].ToString() + ", ");
        //            }

        //            string strCols = SB.ToString().Substring(0, SB.ToString().Length - 2);

        //            DataView dvPrimary = new DataView(ds.Tables[0]);
        //            dvPrimary.RowFilter = " IS_PRIMARY =TRUE ";
        //            if (sidx == "" || sidx == null)
        //            {
        //                sidx = dv[0]["FIELD_NAME"].ToString();
        //            }

        //            string sortExp = sidx + " " + "desc";

        //            //string strQryy = " SELECT " + cols + " FROM ";
        //            //strQryy = strQryy + " (SELECT ROW_NUMBER() OVER (ORDER BY " + sortExp + ") AS Row,";
        //            //strQryy = strQryy + " * FROM  " + dimTableName;
        //            ////for search filter
        //            //if (_search && (query != null || query != ""))
        //            //{
        //            //    strQryy = strQryy + " WHERE " + query.Replace("\"", "'");
        //            //}
        //            //strQryy = strQryy + ") tmp ";

        //            string strDrop = " IF OBJECT_ID('tempdb..#tempRpt') IS NOT NULL DROP TABLE #tempRpt ; ";

        //            Util.WritInfoLog("$ReportData$", "GetGriddataDummy", "Query for dropping temp table", string.Format("Query:{0}", strDrop));
        //            int ret = sqlConn.SaveDataByFactory(strDrop);
        //            Util.WritInfoLog("$ReportData$", "GetGriddataDummy", "End Query for dropping temp table");


        //            string strQry = " SELECT '' AS SOURCE," + cols + " into #tempRpt FROM " + dimTableName + " WHERE ";
        //            strQry = strQry + dvPrimary[0]["FIELD_NAME"].ToString() + " IN ( SELECT MASTER_ID FROM ";
        //            strQry = strQry + dtX.Rows[0]["TABLE_SOURCE"];
        //            if (queryRel != "")
        //            {
        //                strQry = strQry + "  WHERE " + queryRel.Replace("\"", "'");  //+" CRM > 0 AND ERP = 0 AND WEB = 0 "; //
        //            }

        //            if (SourceQuery != "")
        //            {
        //                if (queryRel != "")
        //                {
        //                    strQry = strQry + "  AND " + SourceQuery;  //+" CRM > 0 AND ERP = 0 AND WEB = 0 "; //
        //                }
        //                else
        //                {
        //                    strQry = strQry + "  WHERE " + SourceQuery;  //+" CRM > 0 AND ERP = 0 AND WEB = 0 "; //
        //                }
        //            }

        //            strQry = strQry + "  ) ";
        //            strQry = strQry + "SELECT * FROM (";
        //            strQry = strQry + " SELECT ROW_NUMBER() OVER (ORDER BY " + dvPrimary[0]["FIELD_NAME"].ToString();
        //            strQry = strQry + " , priority " + ") AS Row,* FROM (";
        //            strQry = strQry + " (select Source, " + cols + ",0 as priority from #tempRpt ";
        //            strQry = strQry + " UNION ";
        //            strQry = strQry + " Select Dim.Source," + strCols + ",1 as priority from " + dtX.Rows[0]["TABLE_XREF"];
        //            // strQry = strQry + "  Dim INNER JOIN #tempRpt t " + " ON Dim." + dvPrimary[0]["FIELD_NAME"].ToString();
        //            strQry = strQry + "  Dim INNER JOIN #tempRpt t " + " ON Dim.MASTER_ID";
        //            strQry = strQry + " = t." + dvPrimary[0]["FIELD_NAME"].ToString() + ") ";
        //            strQry = strQry + ")tmp";
        //            strQry = strQry + " ) temp ";

        //            Util.WritInfoLog("$ReportData$", "GetGriddataDummy", "Query for GetGriddata", string.Format("Query:{0}", strQry));
        //            dsdata = sqlConn.GetDataSetFromFactory(strQry);
        //            Util.WritInfoLog("$ReportData$", "GetGriddataDummy", "End Query for GetGriddata");

        //        }

        //        return dsdata;
        //    }
        //    catch (Exception ex)
        //    {
        //        Util.WritErrorLog("$MasterData$", "GetGriddata", "Error in GetGriddata", "", ex);
        //        throw ex;
        //    }
        //}

    }

}
